/*
 * @Author: xuwejie 1529315455@qq.com
 * @Date: 2023-11-08 09:52:41
 * @LastEditors: raoyonggang 11886335+ryg321@user.noreply.gitee.com
 * @LastEditTime: 2024-05-27 17:18:23
 * @FilePath: \my-electron\src\background.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
"use strict";

import { app, protocol, BrowserWindow, Menu, ipcMain, Notification, globalShortcut } from "electron";
import { createProtocol } from "vue-cli-plugin-electron-builder/lib";
import installExtension, { VUEJS_DEVTOOLS } from "electron-devtools-installer";
const isDevelopment = process.env.NODE_ENV !== "production";
const path = require('path');

protocol.registerSchemesAsPrivileged([
  { scheme: "app", privileges: { secure: true, standard: true } },
]);
async function createWindow() {
  const win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: true,  //启用Node继承
      enableRemoteModule: true,//设置开启remote模块
      webviewTag: true, //开启webview标签渲染
      // nodeIntegration: process.env.ELECTRON_NODE_INTEGRATION,
      contextIsolation: !process.env.ELECTRON_NODE_INTEGRATION,
      // contextIsolation: false,
      frame: false,
      useContentSize: true,
      preload: path.join(__dirname, 'preload.js')
    },
    icon: path.join(__dirname, 'logo.ico')
  });

  // 设置强制全屏
  win.setFullScreen(true); 
  // win.openDevTools()
  // 设置默认菜单按钮隐藏
  Menu.setApplicationMenu(null);

  if (process.env.WEBPACK_DEV_SERVER_URL) {
    await win.loadURL(process.env.WEBPACK_DEV_SERVER_URL);
    if (!process.env.IS_TEST) {
      win.openDevTools();
    }
  } else {
    createProtocol("app");
    win.loadURL("app://./index.html");
  }

  globalShortcut.register('F11', () => {
    win.setFullScreen(!win.isFullScreen());
  });
}

app.on("window-all-closed", () => {
  if (process.platform !== "darwin") {
    app.quit();
  }
});

app.on("activate", () => {
  if (BrowserWindow.getAllWindows().length === 0) createWindow();
});

app.on("ready", async () => {
  // if (process.env.NODE_ENV !== 'production') {
  //   require('vue-devtools').install()
  // }
  if (isDevelopment && !process.env.IS_TEST) {
    try {
      await installExtension(VUEJS_DEVTOOLS);
      // BrowserWindow.addDevRoolsExtension(path.resolve(__dirname, '../vue-devtools'))
      
    } catch (e) {
      console.error("Vue Devtools failed to install:", e.toString());
    }
  }
  createWindow();
});

ipcMain.on('notification', (event, message) => {
  new Notification({ title: '通知', body: message }).show();
});
ipcMain.on('message-from-renderer', (event, arg) => {
  console.log(arg); // Prints: 'Hello Main Process'
  // 处理消息
  event.reply('message-from-main', 'Hello Renderer!');
});
// ipcMain.on('toMain', (event, data) => {
//   console.log(data)
//   win.webContents.send('ping', data)
//   // The two lines below return 'undefined' as well in the 'ipcRenderer'
//   //win.webContents.send('fromMain', "Hello IPC Renderer")
//   //event.reply('fromMain', 'Hello IPC Renderer')
// })
// Exit cleanly on request from parent process in development mode.
if (isDevelopment) {
  if (process.platform === "win32") {
    process.on("message", (data) => {
      if (data === "graceful-exit") {
        app.quit();
      }
    });
  } else {
    process.on("SIGTERM", () => {
      app.quit();
    });
  }
}